package LoginSystem.Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 登录窗口
 * @create 2022.5.29
 * @author Administrator
 *
 */

public class LoginDemo01 {
    static int window_width = 1080;//窗口宽度
    static int window_height = 720;//窗口高度
    public void loginFrame(){
        JFrame frame=new JFrame("登录");
        frame.setSize(window_width,window_height);//设置界面大小
        frame.setResizable(false);
        frame.setFocusable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(3);
        JLayeredPane layeredPane=new JLayeredPane();//新建一个分层器
        ImageIcon labIma=new ImageIcon("src/LoginSystem/image/picture.jpg");
        JLabel label=new JLabel();
        label.setIcon(labIma);
        JPanel jpImage=new JPanel();//建立背景层
        jpImage.setBounds(0, 0, window_width,window_height);//设置背景图的大小
        jpImage.add(label);//将装了背景的标签放在面板中
        layeredPane.add(jpImage,JLayeredPane.DEFAULT_LAYER);//将背景图放置在最底层

        JPanel jp2=new JPanel();//建立放置在this中的小面板
        jp2.setLayout(new FlowLayout(FlowLayout.LEFT));//面板布局方式为流式左对齐
        JLabel userNameText=new JLabel("账号：");
        userNameText.setFont(new Font("楷体",Font.BOLD,40));
        userNameText.setForeground(Color.white);
        jp2.add(userNameText);

        JTextField userNameContainer=new JTextField();
        userNameContainer.setFont(new Font("Times New Roman",Font.PLAIN,30));
        userNameContainer.setPreferredSize(new Dimension(340,50));
        jp2.add(userNameContainer);
        jp2.setOpaque(false);

        JLabel PassWordText=new JLabel("密码：");
        PassWordText.setForeground(Color.white);
        PassWordText.setFont(new Font("楷体",Font.BOLD,40));//设置字体大小等
        jp2.add(PassWordText);

        JPasswordField passwordContainer = new JPasswordField();
        passwordContainer.setFont(new Font("Times New Roman",Font.PLAIN,30));
        passwordContainer.setPreferredSize(new Dimension(340,50));//设置强制性大小
        jp2.add(passwordContainer);

        //设置jp2的大小
        jp2.setBounds(240, 250, 550, 120);

        layeredPane.add(jp2,JLayeredPane.MODAL_LAYER);//放在分层最高层

        JButton button1=new JButton("注册");
//实例化按钮重置
        button1.setForeground(Color.white);
        button1.setBackground(new Color(220,20,60,255));
        button1.setFont(new Font("楷体",Font.BOLD,40));
        button1.setBounds(390,380, 120, 50);
        button1.setBorderPainted(false); //设为边界透明
        button1.setBorder(BorderFactory.createRaisedBevelBorder()); //让按钮凸起来
        layeredPane.add(button1,JLayeredPane.MODAL_LAYER);//将放在分层最高层
        JButton button2=new JButton("登陆");
//实例化按钮登陆
        button2.setForeground(Color.white);
        button2.setBackground(new Color(0,128,128,255));
        button2.setFont(new Font("楷体",Font.BOLD,40));
        button2.setBounds(580,380, 120, 50);
        button2.setBorderPainted(false); //设为边界透明
        button2.setBorder(BorderFactory.createRaisedBevelBorder()); //让按钮凸起来
        layeredPane.add(button2,JLayeredPane.MODAL_LAYER);//放在分层最高层

        JDialog jDialog = new JDialog(frame,"提示");
        jDialog.setBounds(1000,500,300,200);
        jDialog.setLayout(new FlowLayout(1));
        JLabel prompt = new JLabel();
        prompt.setFont(new Font("楷体",Font.BOLD,30));

        jDialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                jDialog.setVisible(false);
                prompt.setText("");
            }
        });

        jDialog.add(prompt);
//        jDialog.setVisible(true);


        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(passwordContainer.getPassword());

//                System.out.println(passwordContainer.getPassword().length == 0);
                if("".equals(userNameContainer.getText()) || passwordContainer.getPassword().length == 0){
                    prompt.setText("输入值为空！！！");
                }else {
//                    String S="1234";

                    prompt.setText("注册成功");
                    String fi = "D:\\test.txt";
                    FileWriter fileOutputStream =null;
                    try {
                        fileOutputStream = new FileWriter(fi,true);

                        passwordContainer.getPassword();
                        fileOutputStream.write(userNameContainer.getText()+",");
                        for (int i = 0; i < passwordContainer.getPassword().length; i++) {
                            fileOutputStream.write(passwordContainer.getPassword()[i]);

                        }
                        fileOutputStream.write("\n");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    } finally {
                        try {
                            fileOutputStream.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                }
                jDialog.setVisible(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if("".equals(userNameContainer.getText()) || passwordContainer.getPassword().length == 0){
                    prompt.setText("输入值为空！！！");
                    jDialog.setVisible(true);
                }else {
                    frame.setVisible(false);
                    jDialog.setVisible(false);

                    // prompt.setText("登录成功");
                    new CentreFrame();
                }

            }
        });
//
        frame.add(layeredPane);
        frame.setVisible(true);

    }
}
